package com.woniuxy.controller;

import com.woniuxy.dao.CampRepo;
import com.woniuxy.entity.Camp;
import com.woniuxy.entity.City;
import com.woniuxy.entity.Coordinate;
import com.woniuxy.service.CampService;
import com.woniuxy.service.CoordinateService;
import com.woniuxy.util.MapUtils;
import com.woniuxy.util.Page;
import com.woniuxy.util.Result;
import com.woniuxy.vo.CampVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CampController {
    @Resource
    private CampService campService;
    @Resource
    private CoordinateService coordinateService;
    @Resource
    private CampRepo campRepo;

    @RequestMapping("/camp/selectCamp")
    public Result selectCampByExample(Integer index, Integer number, String cityName, String campName) throws Exception {
        if(campName==""||campName==null){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", index);
            map.put("number", number);
            map.put("cityName", cityName);
            map.put("campName", campName);
            List<CampVO> list = campService.pageSelectCampByExample(map);
            int dataCount = campService.countCampByExample(map);
            System.out.println(dataCount);
            int pageCount = dataCount % number == 0 ? dataCount / number : dataCount / number + 1;
            Page page = new Page(index, dataCount, pageCount, number);
            return new Result("200", "success", page, list);
        }
        //elasticsearch
        List<CampVO> camps = campRepo.selectCampByExampleES(campName);
        int dataCount = camps.size();
        if (dataCount==0){
            return new Result("200", "success", null, null);
        }
        int pageCount = dataCount % number == 0 ? dataCount / number : dataCount / number + 1;
        Page page = new Page(index, dataCount, pageCount, number);
        List<CampVO> list = new ArrayList<>();
        int begin = (index-1)*number;
        for(int i=begin;i<begin+number;i++){
            if (i>=camps.size()-1){
                list.add(camps.get(camps.size()-1));
                break;
            }else {
                list.add(camps.get(i));
            }
        }
        return new Result("200", "success", page, list);

    }

    @RequestMapping("/camp/updateCamp")
    public Result updateCamp(CampVO campVO) throws Exception {
        System.out.println(campVO);
        Camp camp = new Camp();
        camp.setCampId(campVO.getCampId());
        camp.setCampname(campVO.getCampName());
        camp.setAddress(campVO.getAddress());
        camp.setCityId(campVO.getCityId());
        camp.setTel(campVO.getTel());
        camp.setIntroduce(campVO.getIntroduce());
        campService.updateCamp(camp);
        return new Result("200", "success", null, null);
    }

    //    查询所有城市信息
    @RequestMapping("/camp/selectAllCity")
    public Result selectAllCity() throws Exception {
        return new Result("200", "success", null, campService.selectAllCity());
    }

    //    删除营地信息
    @RequestMapping("/camp/deleteCampByCampId")
    public Result deleteCamp(Integer campId) throws Exception {
        campService.deleteCamp(campId);
        return new Result("200", "success", null, null);
    }

    //    查询所有营地
    @RequestMapping("/camp/selectAllCamp")
    public Result selectAllCamp() throws Exception {
        return new Result("200", "success", null, campService.selectAllCamp());
    }
//    新增营地
    @RequestMapping("/camp/insertCamp")
    public Result insertCamp(CampVO campVO) throws Exception{
        Camp camp = new Camp();
        camp.setCampname(campVO.getCampName());
        camp.setAddress(campVO.getAddress());
        camp.setTel(campVO.getTel());
        camp.setIntroduce(camp.getIntroduce());
        camp.setCityId(campVO.getCityId());
        //获取经纬度
        String coordinate = MapUtils.getCoordinate(campVO.getAddress());
        double lng = 0.0;
        double lat = 0.0;
        if(coordinate!=null){
            lng = Double.parseDouble(coordinate.split(",")[0]);
            lat = Double.parseDouble(coordinate.split(",")[1]);
        }
        Coordinate coord = new Coordinate();
        coord.setAddress(campVO.getAddress());
        coord.setCampName(campVO.getCampName());
        List<City> cities = campService.selectAllCity();
        String cityName = "";
        for (City city : cities) {
            if (city.getCityId() == campVO.getCityId()){
                cityName = city.getCityName();
            }
        }
        coord.setCity(cityName);
        coord.setLongitude(lng);
        coord.setLatitude(lat);
        campService.insertCamp(camp);
        coordinateService.insertCoordinate(coord);
        return new Result("200","success",null,null);
    }

//    @RequestMapping("/select")
//    public Result select(String kw) throws Exception{
//        return new Result("200","success",null,campRepo.selectCampByExampleES(kw));
//    }
}
